package online.misscl.lsy.filter;

import com.auth0.jwt.exceptions.JWTVerificationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;
import org.springframework.web.servlet.HandlerExceptionResolver;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * project name: IntelliJ IDEA
 * Date: 2023/3/17 23:54
 *
 * @PackageName: online.misscl.lsy.filter
 * @ClassName: ExceptionHandlerFilter
 * @Author: code mover
 * @Version:
 * @Description:
 */
@Component
public class ExceptionHandlerFilter extends OncePerRequestFilter {

    @Autowired
    @Qualifier("handlerExceptionResolver")
    private HandlerExceptionResolver resolver;

    @Override
    public void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain){
        try {
            filterChain.doFilter(request, response);
        } catch (JWTVerificationException e) {
            // 会交到 Springmvc的全局异常处理器那里
            System.out.println(666);
            resolver.resolveException(request,response,null,e);
        } catch (ServletException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
